package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.ChooseCourse;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentChooseCourseInfo;
import com.grad.why.studentsystem.mapper.ChooseCourseMapper;
import com.grad.why.studentsystem.mapper.CourseMapper;
import com.grad.why.studentsystem.service.CameraService;
import com.grad.why.studentsystem.service.ChooseCourseService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

@Component
@Service(interfaceClass = ChooseCourseService.class)
@Transactional
public class ChooseCourseServiceImpl implements ChooseCourseService {
    @Autowired
    private ChooseCourseMapper chooseCourseMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<CurrentChooseCourseInfo> getChooseCourseInfos(int offset, int limit, String search) {
        try{
            return chooseCourseMapper.getChooseCourseInfos(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getChooseCourseInfosLength(String search) {
        try{
            return chooseCourseMapper.getChooseCourseInfosLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteChooseCourseInfoById(int i) {
        try{
            return chooseCourseMapper.deleteByPrimaryKey(i);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<CurrentChooseCourseInfo> getAllChooseCourseInfos() {
        try{
            return chooseCourseMapper.getAllChooseCourseInfos();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getMaxChooseCourseId() {
        try{
            return chooseCourseMapper.getMaxIChooseCourseId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insertEntity(ChooseCourse chooseCourse) {
        try{
            return chooseCourseMapper.insertSelective(chooseCourse);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getCountOfStudentName(String stuName) {
        try{
            return chooseCourseMapper.getCountOfStudentName(stuName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getStudentIdByStudentName(String stuName) {
        try{
            return chooseCourseMapper.getStudentIdByStudentName(stuName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateChooseCourseInfoByEntity(ChooseCourse chooseCourse) {
        try{
            return chooseCourseMapper.updateByPrimaryKeySelective(chooseCourse);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int checkStudentIsExist(int studentId) {
        try{
            return chooseCourseMapper.checkStudentIsExist(studentId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Student> getStudentByCourseId(Integer cid) {
        try{
            return chooseCourseMapper.getStudentByCourseId(cid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int isExistChooseCourseInfo(ChooseCourse chooseCourse) {
        try{
            return chooseCourseMapper.isExistChooseCourseInfo(chooseCourse);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<ChooseCourse> getChooseCourseByCid(Integer courseid) {
        try{
            return chooseCourseMapper.getChooseCourseByCid(courseid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addStudentInfosByExcelFile(String uploadCurrentFileName) {
        try{
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;
            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }
            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;

            Row title = wb.getSheetAt(0).getRow(0);
            //修改
            if(!title.getCell(0).getStringCellValue().equals("课程编号")||
                    !title.getCell(1).getStringCellValue().equals("课程名称")||
                    !title.getCell(2).getStringCellValue().equals("学生编号")||
                    !title.getCell(3).getStringCellValue().equals("学生名称")||
                    !title.getCell(4).getStringCellValue().equals("平时成绩")||
                    !title.getCell(5).getStringCellValue().equals("测试成绩")||
                    !title.getCell(6).getStringCellValue().equals("期末成绩")){
                return 0;
            }
            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                ChooseCourse chooseCourse = new ChooseCourse();
                                chooseCourse.setChooseid(chooseCourseMapper.getMaxIChooseCourseId() + 1);
                                chooseCourse.setCid(Integer.parseInt(row.getCell(0).getStringCellValue()));
                                chooseCourse.setSid(Integer.parseInt(row.getCell(2).getStringCellValue()));
                                String state = courseMapper.getCourseStateByCid(Integer.parseInt(row.getCell(0).getStringCellValue()));
                                if(state.equals("未开课")){
                                    chooseCourse.setNormalscore(new Float(0));
                                    chooseCourse.setTestscore(new Float(0));
                                    chooseCourse.setFinalscore(new Float(0));
                                    chooseCourse.setState("未开课");
                                }else if(state.equals("结课")){
                                    chooseCourse.setNormalscore((float)row.getCell(4).getNumericCellValue());
                                    chooseCourse.setTestscore((float)row.getCell(5).getNumericCellValue());
                                    chooseCourse.setFinalscore((float)row.getCell(6).getNumericCellValue());
                                    chooseCourse.setState("结课");
                                }else if(state.equals("未结课")){
                                    chooseCourse.setNormalscore(new Float(0));
                                    chooseCourse.setTestscore(new Float(0));
                                    chooseCourse.setFinalscore(new Float(0));
                                    chooseCourse.setState("未结课");
                                }


                                chooseCourseMapper.insertSelective(chooseCourse);
                                InsertCount = InsertCount + 1;

                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<ChooseCourse> getChooseCoursesBySid(int sid) {
        try{
            return chooseCourseMapper.getChooseCoursesBySid(sid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
